package cn.chenhuanming.shh.administer.core.binder;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * Created by chenhuanming on 2017-07-31.
 *
 * @author chenhuanming
 */
@ControllerAdvice(basePackages = {"cn.chenhuanming.shh.administer","org.springframework.data.rest.webmvc"})
public class ControllerBinder {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm-ss");
        binder.registerCustomEditor(LocalDateTime.class,new CustomDateEditor(dateTimeFormat,false));
        binder.registerCustomEditor(LocalDate.class,new CustomDateEditor(dateFormat,false));
        binder.registerCustomEditor(LocalTime.class,new CustomDateEditor(timeFormat,false));

    }
}
